<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.resataurant.Entity.Dish" %>
<%@ page import="java.util.List" %>
<%@ page import="com.resataurant.Service.AdminService" %>
<%@ page import="com.resataurant.Service.Impl.AdminServiceImpl" %>
<%@ page import="com.resataurant.Service.UserService" %>
<%@ page import="com.resataurant.Service.Impl.UserServiceImpl" %>
<%@ page import="com.resataurant.Controller.User.UserSearchServlet" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>餐厅</title>
    <!-- 新 Bootstrap 核心 CSS 文件 -->
    <!-- 新 Bootstrap 核心 CSS 文件 -->
    <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

    <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>

    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <%--<link href="back.css" rel="stylesheet" type="text/css" />--%>

    <script>
        $(document).ready(function() {
            $('.btn-success').click(function(event) {
                event.preventDefault();  // 阻止链接的默认行为

                // 发送AJAX请求到服务器
                $.getJSON($(this).attr('href'), function(response) {
                    // 根据服务器的响应显示模态框
                    if (response.success) {
                        $('#purchaseModalBody').text('购买成功！');
                    } else {
                        $('#purchaseModalBody').text('购买失败：' + response.error);
                    }
                    $('#purchaseModal').modal('show');
                });
            });
        });

    </script>
</head>
<body >
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <nav class="navbar navbar-default" role="navigation">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button> <a class="navbar-brand" href="#">美食网站</a>
                </div>
                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                    <ul class="nav navbar-nav">
                        <form class="navbar-form navbar-left" role="search" method="post" action="<c:url value="/UserSearchServlet"/>">
                            <div class="form-group">
                                <input type="text" class="form-control" name="DishName" />
                            </div>
                            <button type="submit" class="btn btn-default">查询</button>
                            <button type="button" class ="btn btn-default"><a href="">购物车</a></button>
                            <button type="button" class="btn btn-default"><a href="./UserLog.jsp">查询购买记录</a></button>
                        </form>
                    </ul>
                </div>
            </nav>

            <%

                String res= (String) request.getSession().getAttribute("res");
                UserService userService = new UserServiceImpl();
                List<Dish> list=null;
                if(res != null){
                    list = (List<Dish>) request.getSession().getAttribute("list");
                    if(list == null){
                        // Handle the case where "res" is not null but there is no "list" in the session
                        // This could involve setting "list" to a new list, logging an error, etc.
                        // For this example, we'll just create a new list.
                        list = userService.searchDish((String) request.getSession().getAttribute("DishName"));
                    }
                } else {
                    list = userService.searchDish();
                }
                request.getSession().removeAttribute("res");
                request.getSession().removeAttribute("DishName");
                int i=0;
                for (Dish dish : list) {
                    i++;
                    if(i%3==0){
                        out.write("<div class=\"row\">");
                    }
                    out.write(" <div class=\"col-md-4\">\n" +
                            "                    <div class=\"thumbnail\">");
                    out.write(" <img style=\"width: 300px;height: 200px\" alt=\"300x200\" src=\""+dish.getDishImage()+"\"/>");
                    out.write("  <div class=\"caption\">");
                    out.write(" <h3>\n" +
                            dish.getDishName()+
                            "                            </h3>");
                    out.write("   <p>\n" +
                            dish.getDishContent()+
                            "                            </p>");
                    out.write(" <p>\n" +
                            "                                <label>价格：</label>"+dish.getDishPrice() +
                            "                            元</p>");
                    out.write("<p>\n" +
                            "                                <a class=\"btn btn-primary\" href=\"UserDishDetail.jsp?id="+dish.getDishId()+"\">查看详情</a>\n" +
                            "                            </p>");

                    // 添加购买按钮
                    out.write("<p>\n" +
                            "                                <a class=\"btn btn-success\" href=\"BuyDishServlet?id="+dish.getDishId()+"\">购买</a>\n" +
                            "                            </p>");

                    // 添加加入购物车按钮
                    out.write("<p>\n" +
                            "                                <a class=\"btn btn-warning\" href=\"AddToCart.jsp?id="+dish.getDishId()+"\">加入购物车</a>\n" +
                            "                            </p>");

                    out.write("</div>");
                    out.write("</div>");
                    out.write("</div>");
                    if(i%3==0){
                        out.write("</div>");
                    }
                }


            %>


        </div>
    </div>
</div>
<!-- Modal -->
<div class="modal fade" id="purchaseModal" tabindex="-1" role="dialog" aria-labelledby="purchaseModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="purchaseModalLabel">购买结果</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body" id="purchaseModalBody">
                <!-- 购买结果的消息将会在这里显示 -->
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

</body>
</html>
